package com.BlackPhoneModule.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import util.Constant;

import com.BlackPhoneModule.bean.BlackPhoneBean;
import com.BlackPhoneModule.dao.BlackPhoneModuleDao;
import com.UserModule.bean.TbUserBean;
import com.jspsmart.upload.SmartUpload;

public class BlackPhoneModuleServlet  extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		String method= request.getParameter("method");//get the name of method
		PrintWriter out = response.getWriter();
		TbUserBean user = (TbUserBean)request.getSession().getAttribute("User"); 
		BlackPhoneModuleDao blackPhonedao=new BlackPhoneModuleDao();
		if(method.equals("lookBlackPhone")){
			
				List  BlackPhoneList=null;
				
				BlackPhoneList=blackPhonedao.getAllTbBlackPhone("tb_blackPhone", "createTime", "");
				request.getSession().setAttribute("phone", "");
				request.getSession().setAttribute("BlackPhoneList",BlackPhoneList);
				response.sendRedirect("AddressListModule/BlackPhoneList.jsp?s=1");
			
		}else if(method.equals("looksearch")){
				String phone=request.getParameter("phone");
				String tiaojian="";
				if(phone==null || "".equals(phone)){
					
					
				}else{
					tiaojian=" phone like '%"+phone+"%'";
				}
				List  BlackPhoneList=null;
				BlackPhoneList=blackPhonedao.getAllTbBlackPhone("tb_blackPhone","createTime",tiaojian);
				request.getSession().setAttribute("phone", phone);
				request.getSession().setAttribute("BlackPhoneList",BlackPhoneList);
				response.sendRedirect("AddressListModule/BlackPhoneList.jsp?s=1");
			
		}else if(method.equals("deleteOne")){
			String BlackPhoneId=request.getParameter("BlackPhoneId");
			boolean result=blackPhonedao.del("tb_blackPhone", "id", BlackPhoneId);
			if(result){
				request.getSession().setAttribute("resultSM", "黑名单号码删除成功！");
			}else{
				request.getSession().setAttribute("resultSM", "黑名单号码删除失败！");
			}
			List	BlackPhoneList=blackPhonedao.getAllTbBlackPhone("tb_blackPhone", "createTime","");
			request.getSession().setAttribute("BlackPhoneList",BlackPhoneList);
			response.sendRedirect("AddressListModule/BlackPhoneList.jsp?s=1");
		}else if(method.equals("deleteAll")){
			String idList = request.getParameter("idList");
			idList = idList.substring(0, idList.length()-1);
			boolean result=blackPhonedao.delin("tb_blackPhone", "id", idList);
			if(result){
				request.getSession().setAttribute("resultSM", "黑名单号码删除成功！");
			}else{
				request.getSession().setAttribute("resultSM", "黑名单号码删除失败！");
			}
			List	BlackPhoneList=blackPhonedao.getAllTbBlackPhone("tb_blackPhone","createTime","");
			request.getSession().setAttribute("BlackPhoneList",BlackPhoneList);
			response.sendRedirect("AddressListModule/BlackPhoneList.jsp?s=1");
		}else if(method.equals("importBlackPhoneSubmit")){
			
			String realImagePath = Constant.decodeString(request
					.getParameter("realImagePath"));
			String tempPath = request.getRealPath("/")
					+ "upload".replace("\\", "/");
			String result = "";
			File file = new File(tempPath);
			if(!file.isFile()){
				file.mkdirs();
			}
			// new a SmartUpload object
			SmartUpload su = new SmartUpload();
			su.setCharset("UTF-8");// 使用时候最关键的一步
			// 上传初始化
			su.initialize(this.getServletConfig(),request,response);

			// 设定上传限制
			// 1.限制每个上传文件的最大长度。
			su.setMaxFileSize(10000000);
			// 2.限制总上传数据的长度。
			su.setTotalMaxFileSize(20000000);
			// 3.设定允许上传的文件（通过扩展名限制）,仅允许doc,txt文件。
			su.setAllowedFilesList("xls,XLS");
			boolean sign = true;
			// 4.设定禁止上传的文件（通过扩展名限制）,禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。
			try {
				su.setDeniedFilesList("exe,bat,jsp,htm,html");
				// 上传文件
				su.upload();
				// 将上传文件保存到指定目录
				tempPath = tempPath.replace("\\", "/");
				tempPath = tempPath + "/";
				
				su.save(tempPath);
				System.out.println("文件名称"+su.getFiles().getFile(0).getFileName());
				tempPath += "\\" + su.getFiles().getFile(0).getFileName();
				tempPath = tempPath.replace("\\", "/");
				tempPath = tempPath.replace("//", "/");
				System.out.println("tempsssssssssssssPath==="+tempPath);
				
				
				Workbook wb;
				InputStream is2 = new FileInputStream(tempPath);
				wb = Workbook.getWorkbook(is2);
				Sheet sheet = wb.getSheet(0);
				int row = sheet.getRows();
				List<BlackPhoneBean> BlackPhoneList = new ArrayList<BlackPhoneBean>();
				//// 表结构是：手机号码，当前时间
				for(int i = 1;i<row;i++){
					Cell firstCell = sheet.getCell(0, i);
					String firstContent = firstCell.getContents();
				
					
					BlackPhoneBean blackphoneBean = new BlackPhoneBean();
					blackphoneBean.setPhone(firstContent);
					blackphoneBean.setCreateTime(Constant.decodeDate());
					blackphoneBean.setUserid(user.getUserId());
					BlackPhoneList.add(blackphoneBean);
				}
				if(BlackPhoneList!=null&&BlackPhoneList.size()>0){
					boolean bo = new BlackPhoneModuleDao().saveBlackPhoneList(BlackPhoneList);
					if(bo){
						result="黑名单号码导入成功";
					}else{
						result="黑名单号码导入失败";
					}
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			List  BlackPhoneList=null;
			
			BlackPhoneList=blackPhonedao.getAllTbBlackPhone("tb_blackPhone","createTime","");
			request.getSession().setAttribute("phone", "");
			request.getSession().setAttribute("resultSM", result);
			request.getSession().setAttribute("BlackPhoneList",BlackPhoneList);
			response.sendRedirect("AddressListModule/BlackPhoneList.jsp?s=1");
			
		}else if(method.equals("addBlackPhoneSubmit")){
			String blackphone=request.getParameter("blackphone");
			List<BlackPhoneBean> blackphoneList = new ArrayList<BlackPhoneBean>();
			BlackPhoneBean blackphoneBean = new BlackPhoneBean();
			blackphoneBean.setPhone(blackphone);
			blackphoneBean.setCreateTime(Constant.decodeDate());
			blackphoneBean.setUserid(user.getUserId());
			blackphoneList.add(blackphoneBean);
			String result="";
			boolean bo = new BlackPhoneModuleDao().saveBlackPhoneList(blackphoneList);
			if(bo){
				result="黑名单号码新增成功";
			}else{
				result="黑名单号码新增失败";
			}
			
			List  BlackPhoneList=null;
			BlackPhoneList=blackPhonedao.getAllTbBlackPhone("tb_blackPhone","createTime","");
			request.getSession().setAttribute("phone", "");
			request.getSession().setAttribute("resultSM", result);
			request.getSession().setAttribute("BlackPhoneList",BlackPhoneList);
			response.sendRedirect("AddressListModule/BlackPhoneList.jsp?s=1");
		}
		
		
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			// TODO Auto-generated method stub
			
			this.doGet(request, response);
	}
	
	@Override
	public void init() throws ServletException {
		super.init();
	}
	
}
